package com.school.lab.service.impl;

import java.util.List;

import com.school.lab.dao.AclDao;
import com.school.lab.dao.MoudelDao;
import com.school.lab.dao.RoleDao;
import com.school.lab.domain.Acl;
import com.school.lab.domain.Moudel;
import com.school.lab.domain.Role;
import com.school.lab.service.AclService;


public class AclServiceImpl implements AclService {

	private AclDao aclDao;

	private MoudelDao moudelDao;

	private RoleDao roleDao;

	public void setMoudelDao(MoudelDao moudelDao) {
		this.moudelDao = moudelDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setAclDao(AclDao aclDao) {
		this.aclDao = aclDao;
	}

	public void delAcl(Long id) {
		aclDao.delete(id);

	}

	public Acl getAclById(Long id) {
		return aclDao.get(id);
	}

	public void saveAcl(Acl acl) {
		aclDao.save(acl);
	}

	public void updateAcl(Acl acl) {
		aclDao.saveOrUpdate(acl);
	}

	public List<Acl> findAclByRole(Long roleId) {
		return aclDao.findAclByRole(roleId);
	}

	public void saveOrUpdate(Long roleId, Long moudelId, String properties,
			int flag) {
		Acl acl1 = this.getAcl(roleId, moudelId);
		Moudel moudel = moudelDao.get(moudelId);
		Acl acl2 = this.getAcl(roleId, Long.parseLong(moudel.getParent()));
		if (acl1 != null) {
			if ("permission_c".equals(properties)) {
				acl1.setOperaterAdd(flag);
			} else if ("permission_d".equals(properties)) {
				acl1.setOperaterDel(flag);
			} else if ("permission_s".equals(properties)) {
				acl1.setOperaterShow(flag);
			} else if ("permission_u".equals(properties)) {
				acl1.setOperaterUpdate(flag);
			} 
			aclDao.saveOrUpdate(acl1);
			if (acl1.getOperaterAdd() == 0 && acl1.getOperaterDel() == 0
					&& acl1.getOperaterShow() == 0
					&& acl1.getOperaterUpdate() == 0) {				
				aclDao.delete(acl1.getId());
			}
			if(flag==0){
				int moudleFlag=0;
				List<Moudel>  moudelList=moudelDao.findMoudelByParent(moudel.getParent());
				for(Moudel m:moudelList){
					if(this.getAcl(roleId, m.getId())!=null)
						moudleFlag=1;
				}
				if(moudleFlag==0){
					aclDao.delete(acl2.getId());
				}
			}
			
			
		} else {			
			if (acl2 == null) {
				Acl acl3 = new Acl();
				Role role2 = this.roleDao.get(roleId);
				List<Acl> aclList = role2.getAclList();
				Moudel moudelParent = moudelDao.get(Long.parseLong(moudel
						.getParent()));
				acl3.setRole(role2);
				acl3.setMoudel(moudelParent);
				this.aclDao.save(acl3);
				aclList.add(acl3);
				role2.setAclList(aclList);
				this.roleDao.saveOrUpdate(role2);
			}
			Acl acl = new Acl();
			Role role = this.roleDao.get(roleId);
			List<Acl> aclList = role.getAclList();
			if ("permission_c".equals(properties)) {
				acl.setOperaterAdd(flag);
			} else if ("permission_d".equals(properties)) {
				acl.setOperaterDel(flag);
			} else if ("permission_s".equals(properties)) {
				acl.setOperaterShow(flag);
			} else if ("permission_u".equals(properties)) {
				acl.setOperaterUpdate(flag);
			}
			acl.setMoudel(moudel);
			acl.setRole(role);
			this.aclDao.save(acl);
			aclList.add(acl);
			role.setAclList(aclList);
			this.roleDao.saveOrUpdate(role);
		}
	}

	public Acl getAcl(Long roleId, Long moudelId) {
		return aclDao.getAcl(roleId, moudelId);
	}

}
